package controller;
import java.io.IOException;

import cn.edu.scau.cmi.chenmiaopeng.CompositePatternClient;
import cn.edu.scau.cmi.chenmiaopeng.controller.compositePattern.CompositePatternController;
import cn.edu.scau.cmi.chenmiaopeng.hibernateFrame.client.HibernateFrameWorkClient;
import cn.edu.scau.cmi.chenmiaopeng.interProgramming.InterProgrammingController;
import cn.edu.scau.cmi.chenmiaopeng.spring.client.SpringHibernateJavaFxClient;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.layout.Border;
public class ComprehensiveController {
	@FXML
	private AnchorPane mainPane;
	@FXML
    private Button ex1;
	@FXML
    private Button ex2;
	@FXML
    private Button ex3;
	@FXML
    private Button ex4;
	@FXML
    private Button ex5;
    @FXML
    private Button ex6;
    @FXML
    private Button ex7;
    @FXML
    private Button ex8;
    @FXML
    private BorderPane showView;
    private Node pane=null;
    private Border border;
    private Stage nowStage;
    public void setNowStage(Stage s) {
    	nowStage = s;
    }
    public void setBorder(Border b) {
    	border = b;
    }
    @FXML
    public void clickButton(ActionEvent e) {
    	Button clicked = (Button)e.getSource();
//    	showView.setVisible(true);
    	if(clicked==ex1) {
    		System.out.println("显示接口编程模式");
    		try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/interProgramming/InterProgramUI.fxml"));
    	    	Parent root = loader.load();
    	    	addSubPane(root);
//    	    	nowStage.hide();  //hide()之后show()就可以
//    	    	Scene sc = nowStage.getScene();
//    	    	nowStage.show();
    	    	//fireEvent不行
//    	    	WindowEvent close = new WindowEvent(nowStage, WindowEvent.WINDOW_CLOSE_REQUEST);
//    	    	nowStage.fireEvent(close);
    	    	
//    	    	WindowEvent wh = new WindowEvent(nowStage, WindowEvent.WINDOW_HIDING);
//    	    	nowStage.fireEvent(wh);
//    	    	WindowEvent whn = new WindowEvent(nowStage, WindowEvent.WINDOW_HIDDEN);
//    	    	nowStage.fireEvent(whn);
//    	    	WindowEvent ws = new WindowEvent(nowStage, WindowEvent.WINDOW_SHOWING);
//    	    	nowStage.fireEvent(ws);
//    	    	WindowEvent wsn = new WindowEvent(nowStage, WindowEvent.WINDOW_SHOWN);
//    	    	nowStage.fireEvent(wsn);
//    	    	Scene sc = nowStage.getScene();
//    	    	sc.
                //initView:
    	    	InterProgrammingController con = loader.getController();
    			con.judgeFullAndClear();
    	    	
    			}
    	    catch(IOException ex) {
    	    	ex.printStackTrace();
    	    }
    	}
    	else if(clicked==ex2) {
    		System.out.println("现在显示单例模式。");
	    	try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/singleton/Singleton.fxml"));
	    	Parent root = loader.load();
            addSubPane(root);
            }
	    	catch(IOException ex) {
	    		ex.printStackTrace();
	    	}
    	}
    	else if(clicked==ex3) {
    		System.out.println("现在显示多例模式。");
	    	try {
	    	FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/multipleton/Multipleton.fxml"));
	    	Parent root = loader.load();
	    	addSubPane(root);
            }
	    	catch(IOException ex) {
	    		ex.printStackTrace();
	    	}
    	}
    	else if(clicked==ex4) {
    		System.out.println("现在显示工厂方法。");
    		try {
    	    	FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/ui/factory/factoryUI.fxml"));
    	    	Parent root = loader.load();
    	    	addSubPane(root);
                }
    	    catch(IOException ex) {
    	    		ex.printStackTrace();
    	    }
    	}
    	else if(clicked==ex5) {
    		System.out.println("现在显示适配器模式。");
    		try {
    	    	FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/ui/adapter/AdapterUI.fxml"));
    	    	Parent root = loader.load();
    	    	addSubPane(root);
                }
    	    catch(IOException ex) {
    	    		ex.printStackTrace();
    	    }
    	}
    	else if(clicked==ex6) {
    		System.out.println("现在显示组合模式。");
    		try {
    	    	FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/ui/compositePattern/CompositePatternUI.fxml"));
    	    	CompositePatternClient compositeClient = new CompositePatternClient();
    	    	Parent root = loader.load();
    	    	addSubPane(root);
    	    	compositeClient.initTree(root);
    	    	CompositePatternController.setOriginalOrganization();
                }
    	    catch(IOException ex) {
    	    		ex.printStackTrace();
    	    }
    	}
    	else if(clicked==ex7) {
    		System.out.println("现在显示Hibernate框架模式。");
    		try {
    			  FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/ui/Hibernate/HibernateUI.fxml"));
    			  Parent root = loader.load();
    			  Scene scene = new Scene(root, 764, 551);
    			  Stage hibernateStage = new Stage();
    			  hibernateStage.setScene(scene);
    			  hibernateStage.setTitle("Hibernate框架实验");
    			  
    			  
    			  HibernateFrameWorkClient hibernateClient = new HibernateFrameWorkClient();
    			  Stage stage1 = new Stage();
    			  try {
					hibernateClient.start(stage1);
				  } catch (Exception e1) {
					e1.printStackTrace();
				  }
//    			  hibernateClient.initView(loader);
//    			  hibernateStage.showAndWait();
    			}
    			
    			catch(IOException e1) {
    				e1.printStackTrace();
    			}
    	}
    	else if(clicked==ex8) {
    		System.out.println("现在显示Spring模式。");
    		try {
    	    	FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/ui/Spring/SpringHibernateUI.fxml"));
    	    	SpringHibernateJavaFxClient springClient = new SpringHibernateJavaFxClient();
    	    	Parent root = loader.load();
    	    	addSubPane(root);
    	    	springClient.initView(loader);
                }
    	    catch(IOException ex) {
    	    		ex.printStackTrace();
    	    }
    	}
    }
    private void addSubPane(Node newPane) {
    	if(pane!=null) {
    		mainPane.getChildren().remove(pane);
    	}
    	mainPane.getChildren().add(newPane);
    	newPane.setLayoutX(25);
    	newPane.setLayoutY(40);
    	Pane p = (Pane)newPane;
    	p.setBorder(border);
    	//以下两句可以令textArea找到scrollBar
    	mainPane.applyCss();
//    	mainPane.layout();
//    	mainPane.requestLayout();  //要求对整个容器进行刷新
    	pane = newPane;
    }
}
//ObservableList<String> options = FXCollections.observableArrayList("please choose meat:","Pork","Chicken","Beef");
//
//ComboBox<String> comboBox = (ComboBox<String>)loader.getNamespace().get("comboBox");
//
//comboBox.setItems(options);
//comboBox.getSelectionModel().select(0);




//System.out.println("showView的大小为："+showView);

//stage1.setScene(scene);
//stage1.showAndWait();